﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;
using Generator.CusEventArgs;

namespace Generator.Generators
{
    public partial class TableFieldList : GeneratorBase
    {
        public TableFieldList()
        {
            InitializeComponent();
            AddToolItems();
            this.GeneratorName = "Table Infomation";

        }

        private void AddToolItems()
        {
            ToolStripMenuItem item0 = new ToolStripMenuItem("Refresh");
            item0.Tag = 0;
            ToolStripMenuItem item1 = new ToolStripMenuItem("Add Relationship");
            item1.Tag = 1;
            ToolStripMenuItem item2 = new ToolStripMenuItem("Display Information");
            item2.Tag = 2;

            this.AddToolStripItem(item0);
            this.AddToolStripItem(item1);
            this.AddToolStripItem(item2);
        }


        public override void Generate()
        {
            base.Generate();
        }
        public override void Generate(ToolStripItem item)
        {
            base.Generate(item);
            switch (Convert.ToInt32(item.Tag))
            {
                case 0:
                    refreshTable();
                    showRelations();
                    break;
                case 1:
                    showRelationDialog();
                    //if (ShowEditRelationClickedHandler != null)
                    //{
                    //    TableSelectedEventArg arg = new TableSelectedEventArg(this.Table);
                    //    ShowEditRelationClickedHandler(this, arg);
                    //    if (arg.Result)
                    //    {
                    //        showRelations();
                    //    }
                    //}
                    break;
                case 2:
                    if (ShowEditTableInfomationClickedHandler != null)
                    {
                        TableSelectedEventArg arg=  new TableSelectedEventArg(this.Table);
                        ShowEditTableInfomationClickedHandler(this,arg);
                        if (arg.Result)
                        {
                            refreshTable();
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        private void refreshTable()
        {
            lsvTableField.Items.Clear();
            //this.Table = MYSQLUtility.GetTableInfo(this.Connection, this.Table.TableName);

            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);

            if (tableAddin.TableDisplayName.Trim().Length > 0)
            {
                this.GeneratorName = tableAddin.TableDisplayName;
            }

            foreach (FieldInfo field in Table.Fields)
            {
                ListViewItem item = lsvTableField.Items.Add(field.FieldName);
                
                FieldInfoAddin fieldAddin= tableAddin.FindFieldAddin(field.FieldName,field.Comment);
                if (fieldAddin != null)
                {
                    item.SubItems.Add(string.Format("{0}", fieldAddin.DisplayName));
                }
                else
                {
                    item.SubItems.Add(string.Format("{0}", field.Comment));
                }

                item.SubItems.Add(string.Format("{0}", field.FieldType));
                item.SubItems.Add(string.Format("{0}", field.FieldSize));

                item.SubItems.Add(string.Format("{0}", (field.Key == true ? "YES" : "")));
                item.SubItems.Add(string.Format("{0}", (field.Nullabled == true ? "YES" : "")));
                item.SubItems.Add(string.Format("{0}", field.DefaultValue));

                item.SubItems.Add(string.Format("{0}", (tableAddin.IsListField(field.FieldName) == true ?  GlobalValues. CheckBoxStr_Checked : "")));
                item.SubItems.Add(string.Format("{0}", (tableAddin.IsSearchField(field.FieldName) == true ? GlobalValues.CheckBoxStr_Checked : "")));                

            }
        }

        private void lsvTableField_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            
            ListViewHitTestInfo testInfo = lsvTableField.HitTest(e.X, e.Y);
            if (testInfo.SubItem != null)
            {
                int index = testInfo.Item.SubItems.IndexOf(testInfo.SubItem);
                if (index == 7)
                {
                    if (testInfo.SubItem.Text.Length == 0)
                    {
                        testInfo.SubItem.Text = GlobalValues.CheckBoxStr_Checked;
                        tableAddin.AddListField(testInfo.Item.Text);
                    }
                    else
                    {
                        testInfo.SubItem.Text = "";
                        tableAddin.RemoveListField(testInfo.Item.Text);
                    }
                }
                else if (index == 8)
                {
                    if (testInfo.SubItem.Text.Length == 0)
                    {
                        testInfo.SubItem.Text = GlobalValues.CheckBoxStr_Checked;
                        tableAddin.AddSearchField(testInfo.Item.Text);
                    }
                    else
                    {
                        testInfo.SubItem.Text = "";
                        tableAddin.RemoveSearchField(testInfo.Item.Text);
                    }
                }
                if (TableAddinChangedHandler != null)
                    TableAddinChangedHandler(this, EventArgs.Empty);
            }
        }

        private void showRelationDialog()
        {
            if (ShowEditRelationClickedHandler != null)
            {
                TableSelectedEventArg arg = new TableSelectedEventArg(this.Table);
                ShowEditRelationClickedHandler(this, arg);
                if (arg.Result)
                {
                    showRelations();
                }
            }
        }

        private void showRelations()
        {
            var res = from s in this.Connection.Relations
                      where
                          s!=null && (
                          s.PrimaryTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0 ||
                          s.ForeignTable.TableName.ToLower().CompareTo(this.Table.TableName.ToLower()) == 0)
                      select s;

            lstRelation.Items.Clear();
            txtRelation.Clear();
            foreach (var r in res)
            {
                lstRelation.Items.Add(r);
            }

        }

        private void lstRelation_SelectedIndexChanged(object sender, EventArgs e)
        {
            Relation r = (Relation)lstRelation.SelectedItem;
            if (r != null)
            {
                txtRelation.Text = r.GetDiscription();
            }
            else
            {
                txtRelation.Clear();
            }
        }

        public event EventHandler<TableSelectedEventArg> ShowEditRelationClickedHandler;
        public event EventHandler<TableSelectedEventArg> ShowEditTableInfomationClickedHandler;
        public event EventHandler TableAddinChangedHandler;

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Relation r = lstRelation.SelectedItem as Relation;
            if (r != null)
            {
                this.Table.Connection.RemoveRelation(r);
                showRelations();
            }
        }

        private void lstRelation_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Right) return;
            cmRelations.Show(lstRelation, e.X, e.Y);
        }

        private void cmAllInList_Click(object sender, EventArgs e)
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            foreach (var field in this.Table.Fields)
            {
                tableAddin.AddListField(field.FieldName);
            }
            if (TableAddinChangedHandler != null)
                TableAddinChangedHandler(this, EventArgs.Empty);

            refreshTable();
        }

        private void cmAllOutList_Click(object sender, EventArgs e)
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            foreach (var field in this.Table.Fields)
            {
                tableAddin.RemoveListField(field.FieldName);
            }
            if (TableAddinChangedHandler != null)
                TableAddinChangedHandler(this, EventArgs.Empty);
            refreshTable();
        }

        private void cmAllInSearch_Click(object sender, EventArgs e)
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            foreach (var field in this.Table.Fields)
            {
                tableAddin.AddSearchField(field.FieldName);
            }
            if (TableAddinChangedHandler != null)
                TableAddinChangedHandler(this, EventArgs.Empty);
            refreshTable();
        }

        private void cmAllOutSearch_Click(object sender, EventArgs e)
        {
            TableInfoAddin tableAddin = this.Connection.FindTableAddin(this.Table.TableName);
            foreach (var field in this.Table.Fields)
            {
                tableAddin.RemoveSearchField(field.FieldName);
            }
            if (TableAddinChangedHandler != null)
                TableAddinChangedHandler(this, EventArgs.Empty);
            refreshTable();
        }

        private void lsvTableField_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Right) return;
            cmTableFields.Show(lsvTableField, e.X, e.Y);
        }

        private void cmAddRelationShip_Click(object sender, EventArgs e)
        {
            showRelationDialog();
        }





    }
    
}
